package adapter.company;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.ArrayList;

import custom_view.CompanyFeedItemImageView;
import custom_view.MarkableImageView;
import define.API;
import define.Key;
import model.company.CompanyFeedItem;
import model.company.ShotOfCompanyFeedItem;


/**
 * Created by trek2000 on 21/8/2014.
 */
public class GridViewCompanyFeedAdapter extends BaseAdapter {

    /**
     * Data section
     */
    private ArrayList<ShotOfCompanyFeedItem> mAlShotOfCompanyFeedItem = new ArrayList<>();

    /**
     * Interface section
     */

    /**
     * String section
     */
    private int textViewResourceId;
    /**
     * The other section
     */
    private Context mContext;
    private DisplayImageOptions mDio;

    /**
     * @param context
     */
    public GridViewCompanyFeedAdapter(Context context, ArrayList<ShotOfCompanyFeedItem>  mAlshotOfCompanyFeedItem) {
        this.mContext = context;
        this.mAlShotOfCompanyFeedItem = mAlshotOfCompanyFeedItem;

        mDio = new DisplayImageOptions.Builder()
                .delayBeforeLoading(1000)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    /**
     * @return
     */
    public int getCount() {
        return mAlShotOfCompanyFeedItem.size();
    }

    /**
     * @param position
     * @return
     */
    public ShotOfCompanyFeedItem getItem(int position) {
        return mAlShotOfCompanyFeedItem.get(position);
    }

    /**
     * @param position
     * @return
     */
    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int pos, View convertView, ViewGroup parent) {

        /**
         * Begin draw items in Grid View
         */
        CompanyFeedItemImageView companyFeedItemImageView = null;
        if (convertView == null) {  // if it's not recycled, initialize some attributes\
            companyFeedItemImageView = new CompanyFeedItemImageView(mContext);
            companyFeedItemImageView.setLayoutParams(new GridView.LayoutParams(
                    mContext.getResources().getInteger(R.integer.height_file_view),
                    mContext.getResources().getInteger(R.integer.width_file_view)));
            companyFeedItemImageView.setPadding(5, 5, 0, 0);
            companyFeedItemImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            companyFeedItemImageView.setTag(pos);

            convertView = (CompanyFeedItemImageView) companyFeedItemImageView;
        } else {
            companyFeedItemImageView = (CompanyFeedItemImageView) convertView;
            companyFeedItemImageView.setTag(pos);
        }

        /**
         * check item is video or not
         */
        if (mAlShotOfCompanyFeedItem.get(pos).getContentType() == Key.video) {
            ((CompanyFeedItemImageView) companyFeedItemImageView.findViewWithTag(pos)).setVideo(true);
        } else {
            ((CompanyFeedItemImageView) companyFeedItemImageView.findViewWithTag(pos)).setVideo(false);
        }

        try {

            String URL = "";
            if (API.IS_DEV_SITE)
                URL = mAlShotOfCompanyFeedItem.get(pos).getSplash().replace("https", "http");
            else
                URL = mAlShotOfCompanyFeedItem.get(pos).getSplash();

            Enterprise.imageLoader.displayImage(
                    URL,
                    companyFeedItemImageView, mDio,
                    new SimpleImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String imageUri, View view) {
                        }

                        @Override
                        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                        }

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            if (loadedImage != null & view != null) {
                                ((ImageView) view).setImageBitmap(Bitmap.createScaledBitmap(
                                        loadedImage,
                                        mContext.getResources().getInteger(R.integer.width_file_view),
                                        mContext.getResources().getInteger(R.integer.height_file_view),
                                        false));
                            }
                        }
                    }, new ImageLoadingProgressListener() {
                        @Override
                        public void onProgressUpdate(
                                String imageUri, View view, int current, int total) {
                        }
                    });
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        return convertView;
    }

}